package cn.com.infohold.smartcity.fragment.approve_manage;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import cn.com.infohold.smartcity.R;
import cn.com.infohold.smartcity.adapter.Approve_Adapter;
import cn.com.infohold.smartcity.api.HttpEvent;
import cn.com.infohold.smartcity.api.SDK;
import cn.com.infohold.smartcity.api.bean.ApproveBean;
import cn.com.infohold.smartcity.api.bean.LandResult;
import cn.com.infohold.smartcity.api.bean.ListResult;
import cn.com.infohold.smartcity.api.bean.WarrantBean;
import cn.com.infohold.smartcity.data.XmlData;
import cn.com.infohold.smartcity.parent.ParentFragment;
import cn.com.infohold.smartcity.utils.GsonUtil;
import cn.com.infohold.smartcity.utils.RecyclerViewDivider;
import cn.com.infohold.smartcity.utils.RequestCode;
import common.annotation.ViewInject;
import common.dialog.ProgressDialog;
import library.utils.DisplayUtil;

/**
 * Created by Monsoon on 2017/11/3.
 */
public class Frgm_Not_Approve extends ParentFragment{

    @ViewInject(R.id.et_search)
    EditText et_search;
    @ViewInject(R.id.ib_search)
    ImageButton ib_search;
    @ViewInject(R.id.mRecylerview)
    RecyclerView mRecylerview;
    @ViewInject(R.id.btn_add)
    Button btn_add;

    private String userID;
    private int userRole;
    Approve_Adapter mAdapter;
    private List<WarrantBean> mList = new ArrayList<>();
    private boolean isApprove = false;

    @Override
    protected Object inflateView() {
        return R.layout.frgm_search_list;
    }

    @Override
    protected void initView(View anchor) {

        EventBus.getDefault().register(this);

        userID = XmlData.getInstance().getUserInfo().getId();
        userRole = Integer.valueOf(XmlData.getInstance().getUserInfo().getRoles()).intValue();
        btn_add.setVisibility(View.GONE);

        ib_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    private void initRV(){
        int dividerHeight = DisplayUtil.dp2px(0);
        mRecylerview.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecylerview.addItemDecoration(new RecyclerViewDivider(dividerHeight));
        mAdapter = new Approve_Adapter(getContext(),mList,isApprove);
        mRecylerview.setAdapter(mAdapter);
    }

    private void initData() {
        SDK.api().getWarrantList(1, 15, userID, "", userRole, 1, "", "", "").postEvent(RequestCode.REQUEST_CODE_GET_APPROVE_LIST);
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getData(HttpEvent<String> httpEvent){

        int requestCode = httpEvent.getRequestCode();
        if (requestCode != RequestCode.REQUEST_CODE_GET_APPROVE_LIST) {
            return;
        }

        if (httpEvent.getState() != HttpEvent.STATE_SUCCESS) {
            return;
        } else {
            switch (requestCode){
                case RequestCode.REQUEST_CODE_GET_APPROVE_LIST:
                    Type type = new TypeToken<LandResult<ListResult<WarrantBean>>>(){}.getType();
                    LandResult<ListResult<WarrantBean>> result = GsonUtil.fromJson(httpEvent.getData(),type);
                    if (result.getStatus().equals("1")){
                        List<WarrantBean> list = result.getResult().getList();
                        mList.clear();
                        mList.addAll(list);
                        initRV();
                    }

                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
